package cn.qvtu.game.model;

import cn.qvtu.game.util.StaticValue;

import java.awt.image.BufferedImage;
import java.util.List;


public class Person implements Runnable
{
    protected int x;//坐标x
    protected int y;//坐标y
    protected BufferedImage showImage;//人物显示图片
    protected Thread t;//线程
    protected  double moving = 0;//移动帧数
    protected int status=1;//人物状态 默认向右站立
    protected int jumpForce=0;//跳跃力
    protected int jumpMoveDirection = 0;//跳跃移动方向：0垂直/-1向左/1向右

    protected List<BufferedImage> LeftStandImages;//左站立图集
    protected List<BufferedImage> RightStandImages;//右站立图集
    protected List<BufferedImage> LeftRunImages;//左跑图集
    protected List<BufferedImage> RightRunImages;//右跑图集
    protected List<BufferedImage> LeftJumpImages;//左跳图集
    protected List<BufferedImage> RightJumpImages;//右跳图集
    protected List<BufferedImage> LeftAttackImages;//左攻图集
    protected List<BufferedImage> RightAttackImages;//右攻图集
    protected  BackGround backGround;//场景
    private int score;//得分
    public Person(int x,int y,BackGround backGround)
    {
        //构造器
        this.x=x;
        this.y=y;
        this.backGround = backGround;
        setImageList();
        this.t= new Thread(this);
        t.start();
    }
    /**
     * 初始化各种图集
     */
    protected void setImageList()
    {
        LeftStandImages = StaticValue.LeftPersonImgs.subList(0,4);
        RightStandImages = StaticValue.RightPersonImgs.subList(0,4);
        LeftRunImages = StaticValue.LeftPersonImgs.subList(5,9);
        RightRunImages = StaticValue.RightPersonImgs.subList(5,9);
        LeftJumpImages = StaticValue.LeftPersonImgs.subList(6,7);
        RightJumpImages = StaticValue.RightPersonImgs.subList(6,7);
        LeftAttackImages = StaticValue.LeftPersonImgs.subList(9,12);
        RightAttackImages = StaticValue.RightPersonImgs.subList(9,12);
    }
    public void run()
    {
        //线程更新人物
        while (true)
        {
            try
            {
                switch (status)
                {
                    case 1:
                        //向右-站立
                        if(this.moving>3)
                        {
                            //循环集合下标为0-3的图片
                            this.moving=0;//大于3从0开始
                        }
                        this.showImage = RightStandImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.2;
                        break;
                    case 2:
                        //向右-跑
                        if(this.moving>3)
                        {
                            //循环集合下标为5-8的图片
                            this.moving=0;
                        }
                        this.showImage=RightRunImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.5;
                        break;
                    case 3:
                        //向右-跳跃
                        this.moving=0;//循环集合下标为6的图片
                        this.showImage = RightJumpImages.get((int)moving);//设置显示指定下标的图片
                        break;
                    case 4:
                        //向右-攻击
                        if(this.moving>2)
                        {
                            //循环集合下标为9-11的图片
                            this.moving=0;
                        }
                        this.showImage=RightAttackImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.5;
                        if (this.moving == 2)
                        {
                            //设置攻击动画只播放一次，播放完成改变为站立状态
                            this.status=this.status>0?1:-1;
                        }
                        break;
                    case -1:
                        //向左-站立
                        if(this.moving>3)
                        {
                            //循环集合下标为0-3的图片
                            this.moving=0;//大于3从0开始
                        }
                        this.showImage= LeftStandImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.2;
                        break;
                    case -2:
                        //向左-跑
                        if(this.moving>3)
                        {
                            //循环集合下标为5-8的图片
                            this.moving=0;//大于3从0开始
                        }
                        this.showImage= LeftRunImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.5;
                        break;
                    case -3:
                        //向左-跳跃
                        this.moving=0;//循环集合下标为6的图片
                        this.showImage = LeftJumpImages.get((int)moving);//设置显示指定下标的图片
                        break;
                    case -4:
                        //向左-攻击
                        if(this.moving>2)
                        {
                            //循环集合下标为9-11的图片
                            this.moving=0;
                        }
                        this.showImage=LeftAttackImages.get((int)moving);//设置显示指定下标的图片
                        moving+=0.5;
                        if (this.moving == 2)
                        {
                            this.status=this.status>0?1:-1;
                        }
                        break;
                    default:
                        break;
                }
                //人物移动方法
                moveXY();
                //边界控制
                if (x<-100)
                {
                    //最小边界
                    x=-100;
                }
                if (x>750)
                {
                    //最大边界
                    x=750;
                }
                //攻击敌人
                if (this.backGround != null)
                {
                    List<Enemy> allEnemy = this.backGround.getAllEnemy();//获取场景中的所有敌人
                    for (int i=0;i<allEnemy.size();i++)
                    {
                        //遍历所有敌人，看哪个敌人在攻击范围内
                        Enemy enemy = allEnemy.get(i);
                        if (this.status == 4 && (this.x+125) > enemy.getX() && (this.x+125)<(enemy.getX()+250))
                        {
                            enemy.dead();//被向右攻击死掉
                            score+=100;
                        }
                        else if (this.status == -4
                                && (this.x + 125)<(enemy.getX()+400)
                                &&(this.x+125)>(enemy.getX()+256))
                        {
                            enemy.dead();//被向左的攻击死掉
                            score+=100;
                        }
                    }
                }

                Thread.sleep(50);//每50毫秒更新一次
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    public void setX(int x)
    {
        this.x=x;
    }
    public int getX()
    {
        return x;
    }
    public void setY(int y)
    {
        this.y=y;
    }
    public int getY()
    {
        return y;
    }
    public BufferedImage getShowImage()
    {
        if(showImage==null)
        {
            //当显示图片为空获取图片
            showImage = StaticValue.RightPersonImgs.get(0);//获取人物图片集合中的第一张图片
        }
        return showImage;
    }

    public void setShowImage(BufferedImage showImage)
    {
        this.showImage = showImage;
    }
    //右跑
    public void RightRun()
    {
        if(this.status == 3 || this.status ==-3)
        {
            //浮空状态不允许改变状态
            return;
        }
        this.status=2;
    }
    //左跑
    public void LeftRun()
    {
        if (this.status == 3 || this.status ==-3)
        {
            //浮空状态不允许改变状态
            return;
        }
        this.status=-2;
    }
    //停止右跑
    public void StopRightRun()
    {
        if (this.status == 3 || this.status == -3)
        {
            //浮空状态不允许改变状态
            this.jumpMoveDirection = 0;//跳跃结束恢复站立
            return;
        }
        this.status=1;
    }
    //停止左跑
    public void StopLeftRun()
    {
        if (this.status == 3 || this.status ==-3)
        {
            //浮空状态不允许改变状态
            this.jumpMoveDirection = 0;//跳跃结束后恢复站立
            return;
        }
        this.status=-1;
    }
    /**
     * 移动人物坐标(x,y)
     */
    private void moveXY()
    {
        switch (status)
        {
            case 1:
                //向右-站立
            case -1:
                //向左--站立
                break;
            case 2:
                //向右-跑
                x+=10;//人物x坐标向右移动10像素
                break;
            case -2:
                //向左-跑
                x-=10;//人物x坐标向左移动10像素
                break;
            case 3:
            case -3:
                //跳跃
                if(this.jumpMoveDirection>0)
                {
                    x+=10;//跳跃向右移动
                }
                else if(this.jumpMoveDirection<0)
                {
                    x-=10;//跳跃向左移动
                }
                y-=jumpForce;//y坐标随着跳跃力改变
                if (y>305)
                {
                    //305是地面y坐标，跳跃结束
                    y=305;//设置y高度，恢复初始值
                    if (this.jumpMoveDirection>0)
                    {
                        this.status=2;//恢复原来向右移动的状态
                    }
                    else if(this.jumpMoveDirection<0)
                    {
                        this.status = -2;//恢复原来向左移动的状态
                    }
                    else
                    {
                        this.status=this.status>0?1:-1;//恢复站立状态
                    }
                }
                jumpForce--;//跳跃力减小
                break;
            case 4:
            case -4:
                //攻击
                break;
            default:
                break;
        }
    }

    /**
     * 跳跃
     */
    public void Jump()
    {
        //if (this.status != 3 && this.status != -3)
        {
            //非跳跃状态执行
                if (this.status == 2 || this.status == -2)
                {
                    this.jumpMoveDirection = this.status > 0 ? 1 : -1;
                }
                this.status = this.status > 0 ? 3 : -3;//设置状态为跳跃
                this.jumpForce = 15;//设置跳跃力
        }
    }
    /**
     * 攻击
     */
    public void attack()
    {
        //非跳跃状态执行
        //if(this.status != 3 && this.status != -3)
        {
            try
            {
                if (this.status > 0)
                {
                    this.status = 4;
                }
                else
                {
                    this.status = -4;
                }
                Thread.sleep(50);//每50毫秒更新一次
                if(this.status == 4 || this.status == -4)
                {
                    this.status=this.status>0?3:-3;
                }
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    public int getStatus()
    {
        return status;
    }
    public void setStatus(int status)
    {
        this.status = status;
    }
    public BackGround getBackGround()
    {
        return backGround;
    }
    public void setBackGround(BackGround backGround)
    {
        this.backGround = backGround;
    }
    public int getScore()
    {
        return score;
    }

    public void takeTools()
    {
        if (this.backGround != null)
        {
            List<Tools> allTools = this.backGround.getAllTools();
            for (int i = 0; i < allTools.size(); i++)
            {
                Tools tools = allTools.get(i);
                int pw = 100, px = this.x + 140;
                int dw = 20, dx = tools.getX() + 15;
                if ((px > dx && px < (dx + dw)) || (dx > px && dx < (px + pw)))
                {
                    tools.getTools();
                }
            }
        }
    }
}